﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Serialization;

namespace QA_System
{
   public  class XMLNounConnection: IXMLConnectInterface
    {
       static string datafilename;
       static string xmlDatabasePath = QA_System.Properties.Settings.Default.xmlDatabasePath;
       public XMLNounConnection()
       {
           datafilename = xmlDatabasePath + "NounXMLdatabase.xml";
       }

       public string XMLDataFileName
       {
           set { datafilename = value; }
           get { return datafilename; }
       }

       #region IXMLConnectInterface Members

       public string GetWordType(WordStruct word, string filename)
       {
           try
           {
               if (string.IsNullOrEmpty(filename))
               {
                   filename = datafilename;
               }
               string result = "";
               XmlDocument doc = new XmlDocument();
               doc.Load(filename);
               XmlNode node = doc.SelectSingleNode("//Name[w='" + word.Word.ToLower() + "']");
               if (node == null) node = doc.SelectSingleNode("//Body[w='" + word.Word.ToLower() + "']");
               if (node == null) node = doc.SelectSingleNode("//Job[w='" + word.Word.ToLower() + "']");
               if (node != null)
               {
                   if (node.Name == "Body") result = node.Name;
                   else
                       result = node.ParentNode.Name;
               }
               return result;
           }
           catch (Exception ex)
           {
               throw ex;
           }               
       }

       public bool IsBsameCatagoryA(WordStruct wordA, WordStruct wordB)
       {
           try
           {
               bool result = false;
               XmlDocument doc = new XmlDocument();
               doc.Load(datafilename);
               XmlNode nodeA = doc.SelectSingleNode("//Name[w='" + wordA.Word.ToLower() + "']");
               if (nodeA == null) nodeA = doc.SelectSingleNode("//Body[w='" + wordA.Word.ToLower() + "']");
               if (nodeA == null) nodeA = doc.SelectSingleNode("//Job[w='" + wordA.Word.ToLower() + "']");
               if (nodeA != null)
               {
                   foreach (XmlNode node in nodeA.ChildNodes)
                   {
                       if (node.InnerText.Equals(wordB.Word.ToLower()))
                       {
                           result = true;
                           break;
                       }
                   }
               }
               return result;
           }
           catch (XmlException ex)
           {
               throw ex;
           }
       }
       
       

       public bool IsSameType(WordStruct word1, WordStruct word2)
       {
           try
           {
               bool result = false;
               string Parent1 = this.GetWordType(word1, datafilename);
               string Parent2 = this.GetWordType(word2, datafilename);
               if (Parent1.Equals(Parent2) && Parent1!="" && Parent2!="") result = true;
               return result;
           }
           catch (Exception ex)
           { throw ex; }
       }

       #endregion

       

       public bool IsNounHuman(string noun, string databasefile)
       {
           try
           {
               bool result =false;
               XmlDocument doc = new XmlDocument();
               if (string.IsNullOrEmpty(databasefile)) doc.Load(datafilename);
               else doc.Load(datafilename);
               XmlNode humanenode = doc.SelectSingleNode("//People/Name[w='" + noun.ToLower() + "']");
               if (humanenode != null) result = true;
               else
               {
                   humanenode = doc.SelectSingleNode("//People/Job[w='" + noun.ToLower() + "']");
                   if (humanenode != null) result = true;
               }
               return result;
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }

       public bool IsNounHumanName(ref WordStruct noun, string databasefile)
       {
           try
           {
               bool result = false;
               XmlDocument doc = new XmlDocument();
               if (string.IsNullOrEmpty(databasefile)) doc.Load(datafilename);
               else doc.Load(databasefile);
               XmlNode humanenode = doc.SelectSingleNode("//People/Name[w='" + noun.Word.ToLower() + "']");
               if (humanenode != null)
               {
                   result = true;
                   noun.WordSubMean += "People" + " | ";
                   noun.WordSubMean += humanenode.ChildNodes[0].Attributes["gender"].Value +" | ";
               }
               return result;
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }

       public bool IsBpartofA(WordStruct A, WordStruct B)
       {
           try
           {
               try
               {
                   bool result = false;
                   XmlDocument doc = new XmlDocument();
                   doc.Load(datafilename);
                   XmlNode node = doc.SelectSingleNode("//Name[w='" + A.Word.ToLower() + "']");
                   if (node == null) node = doc.SelectSingleNode("//Job[w='" + A.Word.ToLower() + "']");
                   if (node != null)
                   {
                       XmlNode Childnode = doc.SelectSingleNode("//" + node.ParentNode.Name.ToLower() + "/Body[w='" + B.Word.ToLower() + "']");
                       if (Childnode != null) result = true;
                   }
                   return result;
               }
               catch (Exception ex)
               {
                   throw ex;
               }               
           }
           catch (XmlException ex)
           {
               throw ex;
           }
       }

       public bool IsBothAttribute(WordStruct A, WordStruct B)
       {
           try
           {
               try
               {
                   
                   bool result = false;
                   string strA="";
                   string strB="";
                   XmlDocument doc = new XmlDocument();
                   doc.Load(datafilename);
                   //XmlNodeList node = doc.SelectNodes("//Attributes[w='" + A.Word + "']");
                   //XmlNodeList node = doc.SelectNodes("//Attributes[w='" + A.Word + "']");
                   XmlNodeList node = doc.SelectNodes("//Attributes/w");
                   if (node != null)
                   {
                       foreach (XmlNode childnode in node)
                       {
                           XmlNode sibling = childnode.ParentNode.SelectSingleNode("//Attributes[w='" + B.Word.ToLower() + "']/w");
                           //XmlNode sibling2 = childnode.ParentNode.SelectSingleNode("//Attributes[w='" + A.Word + "']/w");
                           //if (sibling.Attributes["catagory"].Value == sibling2.Attributes["catagory"].Value)
                               //result = true;
                           
                           if (childnode.InnerText == B.Word.ToLower())
                               strA = childnode.Attributes["catagory"].Value;
                           if (childnode.InnerText == A.Word.ToLower())
                               strB = childnode.Attributes["catagory"].Value;
                           
                           /*
                           if (childnode.InnerText == B.Word)
                               strA = childnode.Attributes["catagory"].Value;
                           if (childnode.InnerText == A.Word)
                               strB = childnode.Attributes["catagory"].Value;
                           */
                            //if (null != sibling) result = true;
                           //break;
                       }

                       
                   }

                   if (strA!="" && strB!="" && strA==strB)
                       result = true;
                   /*
                   bool result = false;
                   string cataOfA = "";
                   string cataOfB = "";
                   XmlDocument doc = new XmlDocument();
                   doc.Load(datafilename);
                   XmlNodeList nodeA = doc.SelectNodes("//Attributes[w='" + A.Word + "']");
                   if (nodeA != null)
                   {
                       foreach (XmlNode childnodeA in nodeA)
                       {
                           //XmlNode sibling = childnode.ParentNode.SelectSingleNode("//Attributes[w='" + B.Word + "']/w");

                           //if (null != sibling) result = true;
                           if (childnodeA.InnerText == A.Word)
                               cataOfA = childnodeA.Attributes["catagory"].Value;
                           break;
                       }
                   }
                   XmlNodeList nodeB = doc.SelectNodes("//Attributes[w='" + B.Word + "']");
                   if (nodeB != null)
                   {
                       foreach (XmlNode childnodeB in nodeB)
                       {
                           //XmlNode sibling = childnode.ParentNode.SelectSingleNode("//Attributes[w='" + B.Word + "']/w");

                           //if (null != sibling) result = true;
                           if (childnodeB.InnerText == B.Word)
                               cataOfA = childnodeB.Attributes["catagory"].Value;
                           break;
                       }
                   }
                   if (cataOfA == cataOfB)
                   {
                       result = true;
                   }
                   else result = false;
                    * 
                    * */
                   /*
                   string nodeparentA="";
                   if (node != null) nodeparent = node.ParentNode.ParentNode.Name;
                   if (nodeparent != null && nodeparent != "")
                   {
                   }  
                   */  
                   /*
                           XmlNode sibling = doc.SelectSingleNode("//"+ nodeparent+ "//Attributes[w='" + B.Word + "']");
                   if (node != null)
                   {
                       foreach (XmlNode childnode in node)
                       {
                           XmlNode sibling = childnode.ParentNode.SelectSingleNode("//Attributes[w='" + B.Word + "']/w");
                           if (sibling.Attributes["catagory"].Value == childnode.Attributes["catagory"].Value)
                           {
                               result = false;
                               break;
                           }
                           if (null != sibling) result = true;
                           break;
                       }
                   }
                    * */
                   return result;
               }
               catch (Exception ex)
               {
                   throw ex;
               }
           }
           catch (XmlException ex)
           {
               throw ex;
           }
       }

       public bool IsBaAttributeOfA(WordStruct A, WordStruct B)
       {
           try
           {
               try
               {
                   /* code cu
                   bool result = false;
                   XmlDocument doc = new XmlDocument();
                   doc.Load(datafilename);
                   XmlNodeList node = doc.SelectNodes("//Attributes[w='" + A.Word + "']/w");

                   if (node != null)
                   {
                      foreach (XmlNode childnode in node)
                       {
                           XmlNode sibling = doc.SelectSingleNode("//Name[w='" + B.Word + "']");
                           if (null != sibling)
                           {
                              if (childnode.Attributes["catagory"].Value == sibling.ParentNode.Name)
                               {
                                   result = true;
                                   break;
                               }
                           }
                       }
                       
                   }
                   
                    ** */
                   bool result = false;
                   XmlDocument doc = new XmlDocument();
                   doc.Load(datafilename);
                   XmlNode node = doc.SelectSingleNode("//Name[w='" + A.Word.ToLower() + "']/w");
                   string nodeparent="";
                   if (node != null) nodeparent = node.ParentNode.ParentNode.Name;
                   if (nodeparent != null && nodeparent!="")
                   {
                       
                       
                           XmlNode sibling = doc.SelectSingleNode("//"+ nodeparent+ "//Attributes[w='" + B.Word.ToLower() + "']");
                           if (null != sibling)
                           {
                               
                                   result = true;
                                  
                            
                           }
                       
                   }
                   
                   return result;
               }
               catch (Exception ex)
               {
                   throw ex;
               }
           }
           catch (XmlException ex)
           {
               throw ex;
           }
       }


       public string GetUpperCatagory(WordStruct word, string filename)
       {
           try
           {
               if (string.IsNullOrEmpty(filename))
               {
                   filename = datafilename;
               }
               string result = "";
               XmlDocument doc = new XmlDocument();
               doc.Load(filename);
               XmlNode node = doc.SelectSingleNode("//Name[w='" + word.Word.ToLower() + "']");
               if (node == null) node = doc.SelectSingleNode("//Body[w='" + word.Word.ToLower() + "']");
               if (node == null) node = doc.SelectSingleNode("//Job[w='" + word.Word.ToLower() + "']");
               if (node != null)
               {
                   result = node.ParentNode.ParentNode.Name;
               }
               return result;
           }
           catch (XmlException ex)
           {
               throw ex;
           }           
       }

       static public bool IsCompositNoun(string noun, string databasefile)
       {
           try
           {
               bool result = false;
               XmlDocument doc = new XmlDocument();
               if (string.IsNullOrEmpty(databasefile)) doc.Load(datafilename);
               else doc.Load(datafilename);
               XmlNode node = doc.SelectSingleNode("//Name[w='" + noun.ToLower() + "']");
               
               if (node != null)
               {
                   result = true;
               }
               
               return result;
           }
           catch (XmlException ex)
           {
               throw ex;
           }
       }

       public string HumanGender(WordStruct human, string databasename)
       {
           try
           {
               string result = "";
               XmlDocument doc = new XmlDocument();
               if (string.IsNullOrEmpty(databasename)) doc.Load(datafilename);
               else doc.Load(databasename);
               //Vinh sua: vi chi doc node dau tien la John nen gender luon la XO

               XmlNodeList humanenode ;
               humanenode = doc.SelectNodes("//People/Name/w");
               foreach (XmlNode xn in humanenode)
               {
                   if (xn.InnerText == human.Word.ToLower())
                   {
                       
                       
                           result = xn.Attributes["gender"].Value;
                           break;
                      // else
                      // {
                      //     xn = doc.SelectSingleNode("//People/Job[w='" + human.Word + "']/w");
                      //     if (xn != null) result = "X?";
                      // }
                   }
               }
               /*
               while (humanenode!=null)
               {
                   humanenode = doc.SelectNodes("//People/Name[w='" + human.Word + "']/w");
                   if (humanenode.FirstChild.ToString()==human.Word)
                   {
                       break;
                       
                   }

               }
                * */

               /* code cu
               XmlNode humanenode = doc.SelectSingleNode("//People/Name[w='" + human.Word + "']/w");
              
                   if (humanenode != null)
                   {
                       result = humanenode.Attributes["gender"].Value;
                   }
                   else
                   {
                       humanenode = doc.SelectSingleNode("//People/Job[w='" + human.Word + "']/w");
                       if (humanenode != null) result = "X?";
                   }
               */
               //Vinh them: neu ko phai thuoc people, ma thuoc job: hoa sĩ...
               /*
               XmlNodeList jobNode = doc.SelectNodes("//People/Job/w");
               foreach (XmlNode xn in jobNode)
               {
                   if (xn.InnerText == human.Word)
                   {

                       result = "X?";
                       break;
                       // else
                       // {
                       //     xn = doc.SelectSingleNode("//People/Job[w='" + human.Word + "']/w");
                       //     if (xn != null) result = "X?";
                       // }
                   }
               }
                * */
               return result;
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }

       public string NcWord(WordStruct noun, string databasename)
       {
           try
           {
               string result = "";
               XmlDocument doc = new XmlDocument();
               if (string.IsNullOrEmpty(databasename)) doc.Load(datafilename);
               else doc.Load(databasename);
               
               XmlNode node = doc.SelectSingleNode("//Name[w='" + noun.Word.ToLower() + "']");
               
               
               if (node != null)
               {
                   XmlNodeList nodeList = doc.SelectNodes("//" + node.ParentNode.Name.ToLower() + "/Name/w");
                   foreach (XmlNode n in nodeList)
                   {
                       if (n.InnerText == noun.Word.ToLower())
                       {
                           result = n.Attributes["nc"].Value;
                           break;
                       }
                   }
                   //if (null != node.Attributes["nc"]) //result = node.Attributes["nc"].Value;
                   /*
                   foreach (XmlNode n in node)
                   {
                       if (n.ChildNodes.InnerText == noun.Word)
                           result = n.Attributes["nc"].Value;
                   }
                   */
               }
                

               else result = "";
               return result;
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }
    }

   public class XMLVerbConnection : IXMLConnectInterface
   {
       string datafilename;
       static string xmlDatabasePath = QA_System.Properties.Settings.Default.xmlDatabasePath;
       public XMLVerbConnection()
       {
           datafilename = xmlDatabasePath + "VerbXMLdatabase.xml";
       }

       public string XMLDataFileName
       {
           set { datafilename = value; }
           get { return datafilename; }
       }
       #region IXMLConnectInterface Members

       public string GetWordType(WordStruct word, string filename)
       {
           try
           {
               if (string.IsNullOrEmpty(filename))
               {
                   filename = datafilename;
               }
               string result = "";
               XmlDocument doc = new XmlDocument();
               doc.Load(filename);
               XmlNode node = doc.SelectSingleNode("//Headword[w='" + word.Word.ToLower() + "']");               
               if (node != null)
               {
                   if(node.Attributes["Type"]!=null) result = node.Attributes["Type"].Value;
               }
               return result;
           }
           catch (XmlException ex)
           {
               throw ex;
           }             
       }

       public bool IsBsameCatagoryA(WordStruct word1, WordStruct word2)
       {
           try
           {
               
               bool result = false;
               XmlDocument doc = new XmlDocument();
               doc.Load(datafilename);
               XmlNode node = doc.SelectSingleNode("//Headword[w='" + word1.Word.ToLower() + "']");
               if (node != null)
               {
                   foreach (XmlNode no in node.ChildNodes)
                   {
                       if (no.InnerText.Equals(word2.Word.ToLower())) result = true;
                   }
               }
               return result;
           }
           catch (XmlException ex)
           {
               throw ex;
           }             
       }

       public bool IsSameType(WordStruct word1, WordStruct word2)
       {
           try
           {
               bool result = false;
               string Parent1 = this.GetWordType(word1, this.datafilename);
               string Parent2 = this.GetWordType(word2, this.datafilename);
               if (Parent1.Equals(Parent2)) result = true;
               return result;
           }
           catch (Exception ex)
           { throw ex; }
       }

       #endregion

       public string GetTypeofObject(WordStruct verb, string filename)
       {
           try
           {
               if (string.IsNullOrEmpty(filename))
               {
                   filename = datafilename;
               }
               string result = "";
               XmlDocument doc = new XmlDocument();
               doc.Load(filename);
               XmlNode node = doc.SelectSingleNode("//Headword[w='" + verb.Word.ToLower() + "']");
               if (node != null)
               {
                   result = node.Attributes["ObjType"].Value;
               }
               return result;
           }
           catch (XmlException ex)
           {
               throw ex;
           }      
       }

       public bool IsAllowDisjoint(WordStruct verb, string filename)
       {
           try
           {
               if (string.IsNullOrEmpty(filename))
               {
                   filename = datafilename;
               }
               bool result = false;
               XmlDocument doc = new XmlDocument();
               doc.Load(filename);
               XmlNode node = doc.SelectSingleNode("//Headword[w='" + verb.Word.ToLower() + "']");
               if (node != null)
               {
                   result = bool.Parse(node.Attributes["Disjoint"].Value);
               }
               return result;
           }
           catch (XmlException ex)
           {
               throw ex;
           }      
       }

       public string GetTypeofSubject(WordStruct verb, string filename)
       {
           try
           {
               if (string.IsNullOrEmpty(filename))
               {
                   filename = datafilename;
               }
               string result = "";
               XmlDocument doc = new XmlDocument();
               doc.Load(filename);
               XmlNode node = doc.SelectSingleNode("//Headword[w='" + verb.Word.ToLower() + "']");
               if (node != null)
               {
                   result = node.Attributes["SubType"].Value;
               }
               return result;
           }
           catch (XmlException ex)
           {
               throw ex;
           }
       }

       public string GetTypeofContrans(WordStruct verb, string filename)
       {
           try
           {
               if (string.IsNullOrEmpty(filename))
               {
                   filename = datafilename;
               }
               string result = "";
               XmlDocument doc = new XmlDocument();
               doc.Load(filename);
               XmlNode node = doc.SelectSingleNode("//Headword[w='" + verb.Word.ToLower() + "']");
               if (node != null)
               {
                   if(null!=node.Attributes["Require"].Value) result = node.Attributes["Require"].Value;
               }
               return result;
           }
           catch (XmlException ex)
           {
               throw ex;
           }
       }

       public string GetDirect(WordStruct verb, string filename)
       {
           try
           {
               if (string.IsNullOrEmpty(filename))
               {
                   filename = datafilename;
               }
               string result = "";
               XmlDocument doc = new XmlDocument();
               doc.Load(filename);
               XmlNode node = doc.SelectSingleNode("//Headword[w='" + verb.Word.ToLower() + "']");
               if (node != null)
               {
                  if(null!=node.Attributes["Direct"].Value) result = node.Attributes["Direct"].Value;
               }
               return result;
           }
           catch (XmlException ex)
           {
               throw ex;
           }
       }
   }
    //Vinh: VPE
   public class VPEXMLConnection
   {
       string datafilename;
       static string xmlDatabasePath = QA_System.Properties.Settings.Default.xmlDatabasePath;
       public VPEXMLConnection()
       {
           datafilename = xmlDatabasePath + "VPEXMLdatabase.xml";
       }

       public string XMLDataFileName
       {
           set { datafilename = value; }
           get { return datafilename; }
       }
       public bool IsVPE(String word, string filename)
       {
           try
           {
               if (string.IsNullOrEmpty(filename))
               {
                   filename = datafilename;
               }
               
               XmlDocument doc = new XmlDocument();
               doc.Load(filename);
               XmlNodeList nodelist = doc.SelectNodes("//VPE");
               /*
               XmlNode node = doc.SelectSingleNode("//Headword[w='" + word.Word + "']");               
               if (node != null)
               {
                   if(node.Attributes["Type"]!=null) result = node.Attributes["Type"].Value;
               }
                */
               foreach (XmlNode node in nodelist)
               {
                   foreach (XmlNode child in node.ChildNodes)
                   {
                       if (child.InnerText == word)
                           return true;
                   }
               }
               return false;
           }
           catch (XmlException ex)
           {
               throw ex;
           }             
       

      
       }
   }

    // Vinh: SAnaphora
   public class SAXMLConnection
   {
       string datafilename;
       static string xmlDatabasePath = QA_System.Properties.Settings.Default.xmlDatabasePath;
       public SAXMLConnection()
       {
           datafilename = xmlDatabasePath + "SAnaphoraXMLdatabase.xml";
       }

       public string XMLDataFileName
       {
           set { datafilename = value; }
           get { return datafilename; }
       }
       public bool IsSA(String word, string filename)
       {
           try
           {
               if (string.IsNullOrEmpty(filename))
               {
                   filename = datafilename;
               }

               XmlDocument doc = new XmlDocument();
               doc.Load(filename);
               XmlNodeList nodelist = doc.SelectNodes("//SAnaphora");
               /*
               XmlNode node = doc.SelectSingleNode("//Headword[w='" + word.Word + "']");               
               if (node != null)
               {
                   if(node.Attributes["Type"]!=null) result = node.Attributes["Type"].Value;
               }
                */
               foreach (XmlNode node in nodelist)
               {
                   foreach (XmlNode child in node.ChildNodes)
                   {
                       if (child.InnerText.ToLower() == word.ToLower())
                           return true;
                   }
               }
               return false;
           }
           catch (XmlException ex)
           {
               throw ex;
           }
       }

       public bool IsOneAnaphora(String word, string filename)
       {
           try
           {
               if (string.IsNullOrEmpty(filename))
               {
                   filename = datafilename;
               }

               XmlDocument doc = new XmlDocument();
               doc.Load(filename);
               XmlNodeList nodelist = doc.SelectNodes("//OneAnaphora");
               /*
               XmlNode node = doc.SelectSingleNode("//Headword[w='" + word.Word + "']");               
               if (node != null)
               {
                   if(node.Attributes["Type"]!=null) result = node.Attributes["Type"].Value;
               }
                */
               foreach (XmlNode node in nodelist)
               {
                   foreach (XmlNode child in node.ChildNodes)
                   {
                       if (child.InnerText.ToLower() == word.ToLower())
                           return true;
                   }
               }
               return false;
           }
           catch (XmlException ex)
           {
               throw ex;
           }
       }

           
   }

   public class OneAnaXMLConnection
   {
       string datafilename;
       static string xmlDatabasePath = QA_System.Properties.Settings.Default.xmlDatabasePath;
       public OneAnaXMLConnection()
       {
           datafilename = xmlDatabasePath + "OneAnaphoraXMLdatabase.xml";
       }

       public string XMLDataFileName
       {
           set { datafilename = value; }
           get { return datafilename; }
       }
       public bool IsOneAnaphora(String word, string filename)
       {
           try
           {
               if (string.IsNullOrEmpty(filename))
               {
                   filename = datafilename;
               }

               XmlDocument doc = new XmlDocument();
               doc.Load(filename);
               XmlNodeList nodelist = doc.SelectNodes("//OneAnaphora");
               /*
               XmlNode node = doc.SelectSingleNode("//Headword[w='" + word.Word + "']");               
               if (node != null)
               {
                   if(node.Attributes["Type"]!=null) result = node.Attributes["Type"].Value;
               }
                */
               foreach (XmlNode node in nodelist)
               {
                   foreach (XmlNode child in node.ChildNodes)
                   {
                       if (child.InnerText.ToLower() == word.ToLower())
                           return true;
                   }
               }
               return false;
           }
           catch (XmlException ex)
           {
               throw ex;
           }
       }
   }

}
